package com.zhongying.customer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhongying.jf.model.BorrowAutoInventRule;
import com.zhongying.jf.result.Result;
import com.zhongying.jf.service.BorrowAutoInventOperateService;
import com.zhongying.sys.utils.DateUtils;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

/**
 * @author 周 浩
 * @email zhou_eric90@163.com
 * @date 2018年4月3日 上午10:53:34
 * @描述 自动投标设置查询
 */
@Controller
@RequestMapping("/auto_borrow")
public class AutoBorrowRecordController {

	@Autowired
	private BorrowAutoInventOperateService borrowAutoInventOperateService;

	/**
	 * 查看自动投标设置
	 * 
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @param userCode
	 *            用户Id
	 * @param page
	 *            当前页码
	 * @param limit
	 *            每页条数
	 * @return
	 */
	@RequestMapping("/list_records")
	@ResponseBody
	public R listAutoBorrowRecords(String startDate, String endDate, String userCode, Integer page, Integer limit) {
		System.out.println(startDate + "-" + endDate + "-" + userCode + "-" + page + "-" + limit);
		if (userCode == null || page == null || page <= 0 || limit == null || limit <= 0) {
			return R.error(-1, "查询参数不合法");
		}
		if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
			Date date = new Date();
			endDate = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss");
			startDate = DateUtils.format(DateUtils.addSecond(date, -7 * 24 * 60 * 60), "yyyy-MM-dd HH:mm:ss");
		}
		System.out.println(">>>>>>>>>>   " + startDate + "  -  " + endDate);
		BorrowAutoInventRule firstOne = borrowAutoInventOperateService.getFirstRecordByUserCode(userCode);
		Result result = borrowAutoInventOperateService.listAutoTenderOperateRecordByUserCode(userCode, startDate, endDate, page, limit);
		Map<String, Object> model = (Map<String, Object>) result.getModel();
		Integer total = (Integer) model.get("total");
		List<BorrowAutoInventRule> sourceList = (List<BorrowAutoInventRule>) model.get("records");
		List<Map<String, Object>> list = convertVo(sourceList, firstOne);
		Collections.sort(list, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				return -((String) o1.get("operateTime")).compareTo((String) o2.get("operateTime"));
			}
		});
		PageUtils pageUtil = new PageUtils(list, total, limit, page);
		return R.ok().put("page", pageUtil);
	}

	private List<Map<String, Object>> convertVo(List<BorrowAutoInventRule> sourceList, BorrowAutoInventRule firstOne) {
		Long firstId = (firstOne == null) ? 0L : firstOne.getId();
		List<Map<String, Object>> list = new ArrayList<>();
		if (!CollectionUtils.isEmpty(sourceList)) {
			Map<String, Object> vo = null;
			for (BorrowAutoInventRule source : sourceList) {
				vo = new HashMap<>();
				vo.put("id", source.getId());
				vo.put("userCode", source.getUserCode());
				String borrowType = source.getBorrowTypeInfo();
				borrowType = (borrowType == null) ? "" : borrowType;
				String borrowTypeView = "";
				switch (borrowType) {
				case "1":
					borrowTypeView = "信用标、抵押标";
					break;
				case "2":
					borrowTypeView = "信用标";
					break;
				case "3":
					borrowTypeView = "抵押标";
					break;
				default:
					break;
				}
				vo.put("borrowTypeInfo", borrowTypeView);

				byte client = source.getClient();
				String clientView = "";
				switch (client) {
				case 1:
					clientView = "PC";
					break;
				case 2:
					clientView = "微信";
					break;
				case 3:
					clientView = "IOS";
					break;
				case 4:
					clientView = "安卓";
					break;
				default:
					clientView = "无法获取";
					break;
				}
				vo.put("client", clientView);

				BigDecimal minAmount = source.getMinInvestAmount();
				BigDecimal maxAmount = source.getMaxInvestAmount();
				String inventAmount = "";
				if (minAmount == null || maxAmount == null) {
					inventAmount = "未设置";
				} else {
					inventAmount = "[" + minAmount + "~" + maxAmount + ")";
				}
				vo.put("inventAmount", inventAmount);

				Byte minPeriod = source.getMinPeriodLength();
				Byte maxPeriod = source.getMaxPeriodLength();
				String inventPeriod = "";
				if (minPeriod == null || maxPeriod == null) {
					inventPeriod = "未设置";
				} else {
					inventPeriod = "[" + minPeriod + "~" + maxPeriod + "]";
				}
				vo.put("inventPeriod", inventPeriod);

				Byte autoUserCouponByte = source.getAutoUseCoupon();
				int autoUserCoupon = (autoUserCouponByte == null) ? 0 : autoUserCouponByte;
				String useCouponView = "否";
				switch (autoUserCoupon) {
				case 1:
					useCouponView = "是";
					break;
				case 2:
					useCouponView = "否";
					break;
				default:
					break;
				}
				vo.put("useCoupon", useCouponView);

				if (firstId == source.getId()) {
					vo.put("operateType", "创建");
				} else {
					vo.put("operateType", "修改");
				}

				vo.put("operateTime", DateUtils.format(source.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				list.add(vo);
			}
		}
		return list;
	}

}
